\section{filter --- Digital Filter Structure}
\label{fn:filter}
\index{filter()}
\index{digital filter structure}
\index{filtering!digital}
\begin{rail}
Filter : 'filter' '(' Matrix ',' Matrix ',' Matrix ( ','  (Matrix)? )? ')' ;
\end{rail}

\verb+filter+ is a framework that allows you to performing filtering
using  finite impulse response (FIR) or infinite impulse
response (IIR) techniques.  

Each matrix argument must be a real row or column vector (or a scalar,
which will be converted to a one element vector).  The first
argument contains the zeros of the transfer function --- call it \^{b},
containing $nb$ terms from \( [b_{1},b_{2},\ldots,b_{nb}] \).  The next
argument contains the poles of the transfer function --- call it \^{a},
containing $na$ terms from \( [a_{1},a_{2},\ldots,a_{na}] \). \footnote{Some
text books( e.g. Rabiner and Gold, Theory and Application of Digital
Signal Processing) use these same variables in the opposite sense}.

Note that the \( a_{1} \) term must not be zero, as all the other terms
are divided by this term in a preprocessing stage.  If you are
attempting to make a FIR filter (zeros only), this term is normally
set to $1$.

The next argument is the input stream to the filter, usually denoted
as \^{x}, containing $nx$ terms, from\( [x_{1},x_{2},\ldots,x_{nx}] \). 

The last arg is an optional vector of the initial values in each delay bin.
It must have number of terms equal to the maximum of
the length of A or B.

The filter outputs are in a list with elements named \verb+y+,
which is the output of the filter - it has the same number of terms as
the input; and \verb+zf+, which is a vector of the final values in
each bin.

The result is calculated by

\[ y_{n} = b_{1}x_{n} + b_{2}x_{n-1} + \ldots + b_{nb+1}x_{n-nb} - a_{2}y_{n-1} - \ldots - a_{na+1}y_{n-na} \]


\paragraph{Example}
\index{example of filter()}
\index{filter()!example}
\begin{verbatim}
> // We want a bandpass filter, unity gain from 0.2 to 0.4
> // Refer Williams and Taylor, 'Electronic Filter Design Handbook',
> // Example 13.1, page 13-8.  31st order FIR.  Use their co-efficients
> b = [ 0.03820,0.01551,0.08376,-0.12525,-0.05134,0.01481,-0.01461, ...
0.03616,-0.08645,-0.03592,0.00906,0.01462,0.14963,-0.25786,-0.12825, ...
0.41256,-0.12825,-0.25786,0.14963,0.01462,0.00906,-0.03592,-0.08645, ...
0.03616,-0.01461,0.01481,-0.05134,-0.12525,0.08376,0.01551,0.03820]
 b =
 matrix columns 1 thru 6
   0.0382     0.0155     0.0838     -0.125    -0.0513     0.0148  

 matrix columns 7 thru 12
  -0.0146     0.0362    -0.0864    -0.0359    0.00906     0.0146  

 matrix columns 13 thru 18
     0.15     -0.258     -0.128      0.413     -0.128     -0.258  

 matrix columns 19 thru 24
     0.15     0.0146    0.00906    -0.0359    -0.0864     0.0362  

 matrix columns 25 thru 30
  -0.0146     0.0148    -0.0513     -0.125     0.0838     0.0155  

 matrix columns 31 thru 31
   0.0382  
> a = 1;
> x = [1; zeros(99,1)];
> // this is useful for the plot
> t = 0:1:0.01;
> output = filter(b,a,x);
> plot([t',log(fft(x))]);
> plot([t',log(fft(output.y))]);
\end{verbatim}

\newpage






